// Smooth Scrolling: Smooth scrolls to an ID on the current page.
$(function() {
    
    $('a.scroll').on('click', function(event) {
        
        // check top headers' state and correct its offset accordingly
        var isMin = $('#top-header').hasClass('min');
        var offsetCorrection = "-40";
        
        if(isMin) {
            offsetCorrection = "-40";
        } 
        
        var $anchor = $(this);
        
        if($anchor.attr('href') == '#home') {
            offsetCorrection = 170;
        }
        
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top - offsetCorrection
        }, 1500);
        event.preventDefault();
    });
    
    /* Header Scroll Effect */
    var logo = $('#logo-image').offset().top + 90;

    $(window).scroll(function(){    
        if ($(this).scrollTop() > logo){ 
            $("#top-header").addClass("min");
        }
        else{
            $("#top-header").removeClass("min");
        }
    });
    
    $(window).on('scroll', function() {
        var windowScroll = $(window).scrollTop();
        var startHeight = "200";
        
        if (windowScroll > startHeight) {
            scrollHandler();
        }
    });
    
    function scrollHandler () {
        
        // control current state of nav links on scroll
        var currentScroll = $(window).scrollTop();

        var scrolledSections = $('.scrollto').map(function(){
            if ($(this).offset().top < (currentScroll + 100)){
                return $(this);
            }
        });
        
        //geting the last one, the current one on the screen
        var currentSection = scrolledSections[scrolledSections.length-1];
    
        var currentSectionId = currentSection.attr('id');
        
        var targets = $('#top-header nav a');
        var target = $('#top-header nav a[href="#'+currentSectionId+'"]');
        
        targets.removeClass('active');
        target.addClass('active');
        
//        console.log('Current Section: ');
//        console.log(currentSection);
//        console.log('Current Section Id: ' + currentSectionId);
//        console.log('Target: ');
//        console.log(target);
    }
    
    // main header nav active controls
//    $('#top-header nav a').on('click', function() {
//        $(this).parents('nav').find('a').removeClass('active');
//        $(this).addClass('active');
//    });
    
    // set class min to top header on scroll 
    $(document).ready(function(){
        var scrollPos = $(window).scrollTop();
         if (scrollPos >= 50) {
            $("#top-header").addClass("min"); 
        }
    });
    
});

// slick slider config
$(document).ready(function(){
    $('#slider-home').slick({
        slidesToShow: 1,
        slidesToScroll: 1,
        nextArrow: '<span id="sh-next" class="fa fa-angle-right"></span>',
        prevArrow: '<span id="sh-prev" class="fa fa-angle-left"></span>',
        autoplay: true,
        autoplaySpeed: 3000,
        // calls the secondary slider to give prev slide, left to right direction
        onAfterChange: function(){ $('#secondary-slider-home').slickPrev(); } 
    });
    
    $('#secondary-slider-home').slick({
        slidesToShow: 1,
        slidesToScroll: 1,
        arrows: false
    });
    
    $('#sh-next').on('click', function() {
        $('#secondary-slider-home').slickPrev();
    });
    $('#sh-prev').on('click', function() {
        $('#secondary-slider-home').slickNext();
    });
});
(function($) {
    
    var cpCont = $('.call-plan');
    var cpFront = $('.cp-front');
    var cpBack = $('.cp-back');
    var flipButton = $('.flip-button');
    var closeButton = $('.cp-close-button');
    
    var popupPage = $('#popup-page');
    var popupFaq = $('#popup-faq');
    var contact = $('#contact-form');
    var popupCloseButton = $('.popup-close-button');
    
    var userButton = $('#user-button');
    
    $('#terms-button, #privacy-button, .read-more').on('click', function(){
        popupPage.show();
    });
    
    $('.faq-button').on('click', function(){
        popupFaq.show();
    });
    
    $('.popup-close-button').on('click', function() {
        popupPage.hide();
        popupFaq.hide();
        contact.hide();
    });
    
    $('.contact-form-link').on('click', function(e) {
        e.preventDefault();
        
        contact.show();
    });
    
    // hover effect on main tabs
//    $('.etabs a.active .icon-label').show();
    
    // focus on login form on login fixed button click
    $('#login-form input').on('click', function() {
        $('#login-form').css('background-color', 'rgba(204, 204, 204, 0.8)');
    });
    
    $('#user-button').on('click', function() {
        $('#login-form').css('background-color', 'rgba(204, 204, 204, 0.8)');
        $('#login-form input')[0].focus();
    });
    
    $('#login-form').on('focusout', function() {
        $('#login-form').css('background-color', 'rgb(255, 255, 255)');
    });

    
    // slim scroll plugin config
    $('.cp-scroll ul').slimScroll({
        position: 'right',
        height: '415px',
        size: '11px',
        railVisible: true,
        alwaysVisible: true
    });
    
    $('#inner-wrapper-popup-page section').slimScroll({
        position: 'right',
        size: '11px',
        height: "80%",
        railVisible: true,
        alwaysVisible: true
    });
    
    // fix icon labels positions to be horizontally centered
    var labelWidth = $('.icon-label').width();
    $('.icon-label').css('margin-left', labelWidth + "px");
    
    
    // show hide call plans' back element
    flipButton.on('click', function (e) {
        e.preventDefault();
        $(this).parents('.call-plan').find('.cp-back').animate({right: 0});
        $(this).parents('.call-plan').find('.cp-front').animate({right: 1000});
    });
    
    closeButton.on('click', function(e) {
       e.preventDefault();
       $(this).parents('.call-plan').find('.cp-back').animate({right: '-1000px'});
       $(this).parents('.call-plan').find('.cp-front').animate({right: 0}); 
    });
    
}(jQuery));

// easy tabs assaignments and configs
(function($){

    $("#info-tabs").easytabs({
        tabs: $('#info-tabs .etabs > li')
    });
    
    $("#company-tabs").easytabs();
    
    $("#features-inner-tabs").easytabs();
    
    $("#services-tabs").easytabs({
        tabs: $('#services-tabs nav > .etabs > li')
    });
    
    $("#s-video-chat-in-tabs").easytabs();
    
    $("#s-fax-in-tabs").easytabs();
    
    $("#tariffs-tabs").easytabs({
        tabs: $('#tariffs-tabs .etabs > li')
    });
    
    $("#solution-tabs").easytabs({
        tabs: $('#solution-tabs nav > .etabs > li')
    });
    
    $("#faq-services").easytabs({
//        tabs: $('#faq-services-tabs > li')
    });
    
    // quick links open tab
    $('#quick-links a[rel="#callplans-tab"]').click( function() {
        $('#tariffs-tabs').easytabs('select', '#callplans-tab');
    });
    
    $('#quick-links a[rel="#s-virtual-office-tab"]').click( function() {
        $('#services-tabs').easytabs('select', '#s-virtual-office-tab');
    });
    
    $('#quick-links a[rel="#features-tab"]').click( function() {
        $('#info-tabs').easytabs('select', '#features-tab');
    });
    
    $('#quick-links a[rel="#howtouse-tab"]').click( function() {
        $('#info-tabs').easytabs('select', '#howtouse-tab');
    });

}(jQuery));

